;; The first four lines of this file were added by Dracula.
;; They tell DrScheme that this is a Dracula Modular ACL2 program.
;; Leave these lines unchanged so that DrScheme can properly load this file.
#reader(planet "reader.rkt" ("cce" "dracula.plt") "modular" "lang")
(require "Ivolume.lisp")
(require "Istockhelper.lisp")

(module Mvolume

(import Istockhelper)

(include-book "io-utilities" :dir :teachpacks)
  
#| -------------------------------------------|#

#| *** functions to compute volume query ***
|#
;; gets sum of all traded shares of a specific ticker over a range of dates
(defun sum-traded-share-over-range-of-dates (accumulator data-over-range-of-dates)
  (if (consp data-over-range-of-dates)
      (sum-traded-share-over-range-of-dates (+ accumulator
                                               (fifth (cdr (car data-over-range-of-dates))))
                                            (cdr data-over-range-of-dates))
      accumulator))

;; gets volume of a specific ticker over a range of dates
; volume is ouputed as a string of format "Ticker,volatility"
(defun volume-response (ticker start-date end-date ticker-list flat-data-avl)
  (let* ((ticker-data (get-ticker-data ticker ticker-list flat-data-avl))
         (data-over-range-of-dates (get-data-over-range-of-dates start-date end-date ticker-data)))
    (concatenate 'string
                 ticker
                 ","
                 (rat->str (sum-traded-share-over-range-of-dates 0 data-over-range-of-dates) 0))))

;; computes volatility query
; output is a list of strings of volume for each ticker (over a range of dates)
(defun volume-query (start-date end-date query-tickers ticker-list flat-data-avl)
  (if (consp query-tickers)
      (cons (volume-response (car query-tickers) start-date end-date ticker-list flat-data-avl)
            (volume-query start-date
                          end-date
                          (cdr query-tickers)
                          ticker-list
                          flat-data-avl))
      nil))
#| -------------------------------------------|#

(export Ivolume))